package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.ServerDriver;
import com.bocloud.cmp.driver.providers.ali.AliEcsDriver;
import com.bocloud.cmp.driver.providers.intf.ServerProvider;
import com.bocloud.cmp.driver.providers.jd.JdServerDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.providers.os.OSServerDriver;
import com.bocloud.cmp.driver.providers.vmware.VmwareServerDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.OSMigrateModel;
import com.bocloud.cmp.model.ServerModel;
import com.bocloud.cmp.model.SnapshotModel;
import com.bocloud.common.model.BsmResult;

/**
 * 虚拟机操作服务接口实现类
 * 
 * @author weiwei
 *
 */
@Service("serverProvider")
public class ServerProviderImpl implements ServerProvider {

    @Override
    public BsmResult list(String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.list();
        }
    }

    @Override
    public BsmResult create(String vendor, String region, ServerModel model) {
        Butler butler = Configuration.instance().getConfig(vendor);
        ServerDriver driver = null;
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.create(model);
        }
    }

    @Override
    public BsmResult migrate(String vendor, String region, ServerModel model) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.migrate(model);
        }
    }

    @Override
    public BsmResult OSMigrate(String vendor, String region, OSMigrateModel OSMigrate) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
        return driver.OSMigrate(OSMigrate);
    }

    @Override
    public BsmResult remove(String instanceId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.remove(instanceId);
        }
    }

    @Override
    public BsmResult detail(String instanceId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.detail(instanceId);
        }
    }

    @Override
    public BsmResult start(String instanceId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.start(instanceId);
        }
    }

    @Override
    public BsmResult stop(String instanceId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持！");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.stop(instanceId);
        }
    }

    @Override
    public BsmResult reboot(String instanceId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.reboot(instanceId);
        }
    }

    @Override
    public BsmResult joinSGroup(String instanceId, String group, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.joinSGroup(instanceId, group);
        }
    }

    @Override
    public BsmResult leaveSGroup(String instanceId, String group, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.leaveSGroup(instanceId, group);
        }
    }

    @Override
    public BsmResult modify(String vendor, String region, ServerModel model) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case JDYUN:
            driver = new JdServerDriver(butler.getAccessKey(), butler.getSecretKey(), butler.getUrl(), region);
            break;
        case ALIYUN:
            driver = new AliEcsDriver(butler.getAccessKey(), butler.getSecretKey(), region);
            break;
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.modify(model);
        }
    }

    @Override
    public BsmResult snapshot(SnapshotModel snapshotModel, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.snapshot(snapshotModel);
        }
    }

    @Override
    public BsmResult suspend(String instanceId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case VMWARE:
            driver = new VmwareServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "暂不支持");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.suspend(instanceId);
        }
    }

    @Override
    public BsmResult VNCConsole(String vncType, String serverId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        default:
            break;
        }
        if (null == driver) {
            return new BsmResult(false, "ServerDriver is null!");
        } else {
            return driver.VncConsole(vncType, serverId);
        }
    }

    @Override
    public BsmResult resume(String serverId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        default:
            break;
        }

        if (null == driver) {
            return new BsmResult(false, "ServerDriver is null!");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.resume(serverId);
        }
    }

    @Override
    public BsmResult unpause(String serverId, String vendor, String region) {
        ServerDriver driver = null;
        Butler butler = Configuration.instance().getConfig(vendor);
        VendorType type = VendorConverter.convert(butler.getCategory());
        switch (type) {
        case OPENSTACK:
            driver = new OSServerDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
                    butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
            break;
        default:
            break;
        }

        if (null == driver) {
            return new BsmResult(false, "ServerDriver is null!");
        } else if (!driver.accessable()) {
            return new BsmResult(false, "用户鉴权失败！");
        } else {
            return driver.unpause(serverId);
        }
    }

}
